import 'package:flutter/material.dart';

import '../../base/foundation/jd_colors.dart';

class JDButton extends StatelessWidget {
  const JDButton({
    super.key,
    this.text = '',
    this.backgroundColor,
    this.disabledBackgroundColor,
    this.minHeight = 48.0,
    this.minWidth = double.infinity,
    this.padding = const EdgeInsets.symmetric(horizontal: 16.0),
    this.radius = 2.0,
    this.eachRadius,
    this.textStyle,
    this.side = BorderSide.none,
    this.selectedBackgroundColor = JDColors.disableYellow,
    this.selected = false,
    this.onPressed,
  });

  final String text;
  final TextStyle? textStyle;
  final Color? backgroundColor;
  final Color? disabledBackgroundColor;
  final double? minHeight;
  final double? minWidth;
  final VoidCallback? onPressed;
  final EdgeInsetsGeometry padding;
  final double radius;
  final BorderRadius? eachRadius;

  final BorderSide side;
  final bool selected;
  final Color selectedBackgroundColor;

  @override
  Widget build(BuildContext context) {
    return TextButton(
        onPressed: onPressed,
        style: ButtonStyle(
          // 文字颜色
          // foregroundColor: MaterialStateProperty.resolveWith(
          //   (states) {
          //     if (states.contains(MaterialState.disabled)) {
          //       return disabledTextColor ?? Colors.white;
          //     }
          //     return textColor ?? Colors.white;
          //   },
          // ),
          // 背景颜色
          backgroundColor: MaterialStateProperty.resolveWith((states) {
            if (states.contains(MaterialState.disabled)) {
              return disabledBackgroundColor ?? JDColors.disableYellow;
            }
            return selected
                ? selectedBackgroundColor
                : (backgroundColor ?? JDColors.appMain);
          }),
          // 水波纹
          overlayColor: MaterialStateProperty.resolveWith((states) {
            return Colors.transparent;
          }),
          // 按钮最小大小
          minimumSize: (minWidth == null || minHeight == null)
              ? null
              : MaterialStateProperty.all<Size>(Size(minWidth!, minHeight!)),
          padding: MaterialStateProperty.all<EdgeInsetsGeometry>(padding),
          shape: MaterialStateProperty.all<OutlinedBorder>(
            RoundedRectangleBorder(
              borderRadius: eachRadius ?? BorderRadius.circular(radius),
            ),
          ),
          side: MaterialStateProperty.all<BorderSide>(side),
        ),
        child: Text(
          text,
          style: textStyle ??
              const TextStyle(color: Color(0xFF222222), fontSize: 16),
        ));
  }
}

/// 默认字号18，白字蓝底，高度48
class TDSelectButton extends StatelessWidget {
  const TDSelectButton({
    super.key,
    this.text = '',
    this.textColor = Colors.black26,
    this.selectedTextColor = Colors.black,
    this.backgroundColor = Colors.grey,
    this.selectedBackgroundColor = Colors.blue,
    this.selected = false,
    this.minHeight = 48.0,
    this.minWidth = 40,
    this.padding = const EdgeInsets.symmetric(horizontal: 16.0),
    this.radius = 2.0,
    this.side = BorderSide.none,
    this.textStyle,
    required this.onPressed,
  });

  final String text;
  final Color textColor;
  final Color selectedTextColor;
  final Color backgroundColor;
  final bool selected;
  final Color selectedBackgroundColor;
  final double? minHeight;
  final double? minWidth;
  final VoidCallback? onPressed;
  final EdgeInsetsGeometry padding;
  final double radius;
  final BorderSide side;
  final TextStyle? textStyle;

  @override
  Widget build(BuildContext context) {
    return TextButton(
        onPressed: onPressed,
        style: ButtonStyle(
          // 文字颜色
          foregroundColor: MaterialStateProperty.resolveWith(
            (states) {
              return selected ? textColor : selectedTextColor;
            },
          ),
          // 背景颜色
          backgroundColor: MaterialStateProperty.resolveWith((states) {
            return selected ? selectedBackgroundColor : backgroundColor;
          }),
          // 水波纹
          overlayColor: MaterialStateProperty.resolveWith((states) {
            return Colors.transparent;
          }),
          // 按钮最小大小
          minimumSize: (minWidth == null || minHeight == null)
              ? null
              : MaterialStateProperty.all<Size>(Size(minWidth!, minHeight!)),
          padding: MaterialStateProperty.all<EdgeInsetsGeometry>(padding),
          shape: MaterialStateProperty.all<OutlinedBorder>(
            RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(radius),
            ),
          ),
          side: MaterialStateProperty.all<BorderSide>(side),
        ),
        child: Text(
          text,
          style: textStyle ?? const TextStyle(fontSize: 18),
        ));
  }
}
